<?php
$prg = isset($program) ? $program : 'unknown';
$form = new VoucherForm($prg);

class VoucherForm {

    private $error;

private $valid;


$requiredFields = array(
        'fstname' => 'STRING','lname' => 'STRING','tel' => 'INT',
        'mail' => 'MAIL','kodikos' => 'STRING'
);

function showError ($name)
{
    if (array_key_exists($name, $this->error))
    {
        $error = '<tr> %s </tr>';
        printf($error, $error[$name]);
    }
    else
        return;
}

function showValue ($name)
{
    if (array_key_exists($name, $valid))
    {
        echo $valid[$name];
    }
    else
        return;
}

function checkSet ($name)
{
    if (isset($_POST[$name]) && ! empty($_POST[$name]))
        return true;
    return false;
}

function setVariable ($name, $type, $default = '')
{
    $set;
    switch ($type)
    {
        case 'INT':
            $set = preg_replace('#[^0-9]#i', '', $_POST[$name]);
            if (! empty($set))
                $valid[$name] = $set;
            else
                $error[$name]=get
            break;
        case 'STRING':
            break;
        case 'MAIL':
            break;
        default:
            $set = $default;
            break;
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    foreach ($requiredFields as $name => $type)
    {
        if (checkSet($name))
        {
            setVariable($name, $type);
        }
    }
}
?>

<?php
// The HTML form as a display function

?>
<div id="form">
  <form
    action="../pageParts/voucherForm.php"
    method="post"
    id="voucherForm">
    <input
      type="hidden"
      name="prog"
      value="<?php echo isset($prog)? $prog : '';  ?>" />
    <div id="errorMsg">
                                 <?php echo $args["error"]; ?>
                            </div>
    <p>
      Τα πεδία με αστερίσκο (<b style="color: #de5b22;">*</b>) είναι
      υποχρεωτικά.
    </p>
    <h3>Στοιχεία Επικοινωνίας</h3>
    <table title="">
      <tr>
        <td><label for="firstname">Όνομα<code>
              <b style="color: #de5b22;">*</b>
            </code></label></td>
        <td><input
          type="text"
          id="firstname"
          name="fstname"
          required="required"
          value=""
          onblur="validateChars(document.getElementById('firstname').value,'firstname')" />
        </td>
      </tr>
      <?php showError('fstname');?>
      <tr>
        <td><label for="lastname">Επίθετο<code>
              <b style="color: #de5b22;">*</b>
            </code></label></td>
        <td><input
          type="text"
          id="lastname"
          name="lname"
          value=""
          required="required"
          onblur="validateChars(document.getElementById('lastname').value,'lastname')" />
        </td>
      </tr>
      <?php showError('lname');?>
      <tr>
        <td><label for="tele">Τηλέφωνο<code>
              <b style="color: #de5b22;">*</b>
            </code></label></td>
        <td><input
          type="text"
          id="tele"
          name="tel"
          value="<?php ?>"
          maxlength=14
          required="required"
          onblur="validateNums(document.getElementById('tele').value, 'tele')" />
        </td>
      </tr>
      <?php showError('tel');?>
      <tr>
        <td><label for="mobile">Κινητό</label></td>
        <td><input
          type="text"
          id="mobile"
          name="mbl"
          value="<?php ?>"
          maxlength=14
          onblur="validateNums(document.getElementById('mobile').value, 'mobile')" />
        </td>
      </tr>
      <?php showError('mbl');?>
      <tr>
        <td><label for="mail">E-mail<?php if($args["prog"] !== 'Security') echo '<code><b style="color:#de5b22;">*</b></code>';?></label></td>
        <td><input
          type="email"
          id="mail"
          name="mail"
          placeholder="example@example.com"
          value="<?php ?>"
          required="required" /></td>
      </tr>
      <?php showError('mail'); ?>
      <tr>
        <td></td>
      </tr>
    </table>
    <br />
    <h3>Πληροφορίες</h3>
    <br />
    <table title="">

                        <?php $name = ($prg == 'turism') ? 'ΣΕΤΕ' : 'ΚΑΥΑΣ'?>
                                <tr>
        <td><label for="kodikos">Κωδικός <?php echo $name; ?><code>
              <b style="color: #de5b22;">*</b>
            </code></label>
          <ol style="list-style: none; float: right;">
            <li class="nav_title">
              <img
                alt="help"
                src="../css/images/help.png" />
              <div class="nav_sub">
                <p>Ο αριθμος <?php echo $name; ?> που λάβατε όταν σημπληρώσατε την αίτηση</p>
                <p>Μήκους 2 χαρακτήρων ακολουθούμενων απο 24 ψηφία, χωρίς κενά.</p>
                <p>Παράδειγμα: YU555552345698758678235424</p>
              </div>
            </li>
          </ol></td>
        <td><input
          type="text"
          id="kodikos"
          name="kodikos"
          value=""
          maxlength=26
          required="required"
          onblur="" /></td>
      </tr>
      <?php showError('kodikos');?>
      <tr>

    </table>
    <br /> <br />
    <div id="result">
      <input
        style="margin-left: 50px;"
        type="reset"
        value="Καθαρισμός"
        class="more" />
                          <?php
                        if ($args["result"] == "")
                        {
                            ?><input
        style="margin-left: 50px;"
        type="submit"
        value="Αποστολή"
        class="more"
        onclick="onSend()" /><?php }else {print ("<h5>". htmlEntities($args["result"], ENT_QUOTES | ENT_IGNORE,"UTF-8")."</h5>"); }?>

                       </div>
  </form>
  <script type="text/javascript">
    var frm = $('#voucherForm');
    frm.submit(function () {
        $('#result').empty().append('Παρακαλώ περιμένετε, η φόρμα σας είναι υπο επεξεργασία');
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                   $( "#form" ).empty().append( data );
                   $(".nav_sub").hide();
                    }
              });
            return false;
            });
</script>
</div>
<?php ?>

<?php
// The call that displays the form with (default, get method)

?>
}